package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 购物车
 */
@RestController
@Slf4j
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     */
    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        //查询当前菜品或者套餐是否在购物车中
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart != null) {
            //如果已经存在，就在原来数量基础上加一
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());//设置用户id
            shoppingCartService.save(shoppingCart);
        }
        return R.success("添加购物车成功");
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.orderByAsc("create_time");
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("clean")
    public R<String> clean() {

        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

  /**
  * 删除购物车
  * @param shoppingCart
  * @return
  */
  @PostMapping("sub")
   public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
       //查询当前菜品或者套餐是否在购物中
       QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId());
       queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
       queryWrapper.eq("user_id", BaseContext.getCurrentId());
       ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
       if (cart.getNumber() > 0) {
           cart.setNumber(cart.getNumber() - 1);
           shoppingCartService.updateById(cart);
       } else {
          shoppingCartService.removeById(cart.getId());
       }
       return R.success("删除成功");
   }
}